/**
 * @author
 * @createtime
 * @class OrganizationForm
 * @extends Ext.Window
 * @description Organization表单
 * @company 掌新软件
 */
OrganizationForm = Ext.extend(Ext.Window, {
	// 构造函数
	constructor : function(_cfg) {
		Ext.applyIf(this, _cfg);

		if (this.demId == '所有维度')
			this.demId = null;
		// 必须先初始化组件
		this.initUIComponents();
		OrganizationForm.superclass.constructor.call(this, {
					id : 'OrganizationFormWin',
					layout : 'fit',
					items : this.formPanel,
					modal : true,
					height : 400,
					width : 500,
					maximizable : true,
					title : '组织详细信息',
					buttonAlign : 'center',
					buttons : [{
								text : '保存',
								iconCls : 'btn-save',
								scope : this,
								handler : this.save
							}, {
								text : '取消',
								iconCls : 'btn-cancel',
								scope : this,
								handler : this.close
							}]
				});
	},// end of the constructor
	// 初始化组件
	initUIComponents : function() {
		this.formPanel = new Ext.FormPanel({
			layout : 'form',
			bodyStyle : 'padding:10px',
			border : false,
			autoScroll : true,
			defaults : {
				anchor : '96%,96%'
			},
			defaultType : 'textfield',
			items : [{
						name : 'department.depId',
						xtype : 'hidden',
						value : this.depId == null ? '' : this.depId
					}, {
						name : 'department.parentId',
						xtype : 'hidden',
						value : this.orgSupId ? this.orgSupId : 0
					}, {
						fieldLabel : '组织名称',
						name : 'department.depName',
						allowBlank : false,
						maxLength : 128
					}, {
						fieldLabel : '组织全称',
						name : 'department.fullName',
						allowBlank : false
					}, {
						fieldLabel : '组织描述',
						xtype : 'textarea',
						name : 'department.depDesc',
						maxLength : 500
					}, {
						fieldLabel : '上级组织',
						readOnly : true,
						value : this.supOrgName
					}, {
						fieldLabel : '类型',
						name : 'department.orgType',
						xtype : 'combo',
						editable : false,
						triggerAction : 'all',
						store : App.orgTypeStore
					}, {
						xtype : 'fieldcontainer',
						fieldLabel : '分管领导',
						layout : 'hbox',
						items : [{
									xtype : 'textfield',
									readOnly : true,
									width : 330,
									name : 'chargeNames',
									value : this.chargeNames
								}, {
									margin : '0 0 0 5',
									xtype : 'button',
									text : '选择',
									scope : this,
									iconCls : 'btn-select',
									handler : function() {
										var fPanel = this.formPanel;
										UserSelector.getView({
											callback : function(ids, fullnames) {
												fPanel
														.getCmpByName('chargeNames')
														.setValue(fullnames);
												fPanel
														.getCmpByName('chargeIds')
														.setValue(ids);
											}
										}, false).show();
									}
								}]
					},{
						 fieldLabel : '组织代码',
						 name : 'department.code'
					}, {
						name : 'chargeIds',
						xtype : 'hidden',
						value : this.chargeIds
					}, {
						fieldLabel : '序号',
						xtype : 'numberfield',
						name : 'department.sn',
						value : this.sn == null ? 0 : this.sn
					}]
		});
		// 加载表单对应的数据
		if (this.depId != null && this.depId != 'undefined') {
			this.formPanel.loadData({
						url : __ctxPath + '/system/getDepartment.do?depId='
								+ this.depId,
						root : 'data',
						preName : 'department'
					});
		}

		var fpStore = this.formPanel.getCmpByName('department.orgType').store;
		if (this.isAdd == true) {
			if (this.orgType == 1 || this.orgType == 0) {
				// 如果当前是集团或机构
				// fpStore.remove(fpStore.getAt(0));
				this.formPanel.getCmpByName('department.orgType').setValue(1);
			} else if (this.orgType == 2) {
				// 如果当前单位
				// fpStore.remove(fpStore.getAt(0));
				fpStore.remove(fpStore.getAt(0));
				this.formPanel.getCmpByName('department.orgType').setValue(2);
			} else {
				this.formPanel.getCmpByName('department.orgType').setValue(1);
			}
		}
	},// end of the initcomponents

	// 重置
	reset : function() {
		this.formPanel.getForm().reset();
	},
	// 保存记录
	save : function() {
		$postForm({
					formPanel : this.formPanel,
					scope : this,
					url : __ctxPath + '/system/saveDepartment.do',
					callback : function(fp, action) {
						if (this.callback) {
							this.callback.call(this.scope);
							this.close();
							return;
						}
						var gridPanel = Ext.getCmp('OrganizationGrid');
						if (gridPanel != null) {
							gridPanel.getStore().reload();
						}
						this.close();
					}
				});
	}// end of save

});